//
//  ImageUtil.m
//  jinneng
//
//  Created by mac on 2020/8/10.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "ImageUtil.h"

@implementation ImageUtil
+ (NSString *) image2DataURL: (UIImage *) image{
    NSData *imageData = nil;

    NSString *mimeType = nil;

    

    if ([self imageHasAlpha: image]) {

        imageData = UIImagePNGRepresentation(image);

        mimeType = @"image/png";

    } else {

        imageData = UIImageJPEGRepresentation(image, 1.0f);

        mimeType = @"image/jpeg";

    }

    

    return [NSString stringWithFormat:@"data:%@;base64,%@", mimeType,

            [imageData base64EncodedStringWithOptions: 0]];
}

+ (BOOL) imageHasAlpha: (UIImage *) image
{

    CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image.CGImage);

    return (alpha == kCGImageAlphaFirst ||

            alpha == kCGImageAlphaLast ||

            alpha == kCGImageAlphaPremultipliedFirst ||

            alpha == kCGImageAlphaPremultipliedLast);

}
@end
